<?php 
/******* 
 * Stephen Hitchner (steve@number41media.com) 
 * db_mysql.class 
 *
 * Created:  [03/2002] 
 * Revised:  [01/2005]
 *
 * Updated for PHP5
 * Requires database.inc
 *
 * Db Class Abstraction 
 * Handles the connection to mysql database defined with connection string 
 * Example DSNs 
 * engine://user:password@host/database   - db connection 
 * engine://user:password@host/database?persistant=true  - persistant db connection 
 *  
 * Example Usage: 
 * $DSN = "mysql://steve:qwerty@localhost/my_database"; 
 * $query = "SELECT * FROM users"; 
 * 
 * $db = Database::create($DSN)
 * ... 
 * $results = $db->select($query); 
 * ... 
 * $results->get_numrow(); 
 * $results->get_row(); 
 * ... 
 * $db->close(); 
 * 
 * Select function returns a Results object. The user handles results with Results 
 * class member functions 
 * 
 * To view errors simply define a MYSQL_LOG eg 
 * define("MYSQL_LOG", "/path/to/log"); 
 * using the command 
 * #tail -f /path/to/log 
 * You can view the errors as they happen 
 */ 
require_once('Log.class.php');

/******************************************************* 
 * Database Class 
 *******************************************************/ 
class Database {
	private function __construct() { 
	}
	//----------------------------------------------------- 
	public static function create($dsn) {
		if ( Log::isDebug() ) {
			$info = parse_url($dsn);
		} else {
			$info = @parse_url($dsn);
		}
		$info['port'] = $info['port'];
		$info['type'] = strtolower($info['scheme']);
		$info['password'] = $info['pass'];
		$info['database'] = str_replace("/","",$info['path']);

		if ( $info['query'] == "persistant=true" ) {
			$info['persistant'] = true;
		} else {
			$info['persistant'] = false;
		}
		unset($info['query']);
		unset($info['scheme']);
		unset($info['pass']);
		unset($info['path']);

		$db_driver = 'DB_' . ucfirst(isset($info['type']) ? $info['type'] : 'mysql');
		Log::debug("Using $db_driver");
		if ( Log::isDebug() ) {
			require_once($db_driver . '.class.php');
			return new $db_driver($info);
		} else {
			@require_once($db_driver . '.class.php');
			return new $db_driver($info);
		}
	}
} 

/******************************************************* 
 * Abstract Database Class 
 *******************************************************/ 
abstract class DB_Abstract { 
	protected $_dbi; 
	protected $_info; 
	protected $_connected;
   
	//***************************************************** 
	// Private Connection Function Constructor, Destuctor
  	//----------------------------------------------------- 
	function __construct($info) { 
		$this->_dbi = 0; 
		$this->_info = $info; 
		$this->_connected = false; 
	}

	function __destruct() {}

	function __toString() {
		return print_r( $this, true );
	}
 
	//***************************************************** 
	// Private Functions 
  	//----------------------------------------------------- 
	abstract protected function open();
	//-----------------------------------------------------
	abstract protected function close();
	//-----------------------------------------------------
	public function isConnected() { 
    		if ( $this->_connected ) { 
			Log::debug('[isConnected] true');
      			return true; 
    		} else { 
			Log::debug('[isConnected] false');
      			return false; 
    		} 
  	} 

  	//***************************************************** 
  	// Public Query Functions 
	//-----------------------------------------------------
	abstract public function select($query); 
	//-----------------------------------------------------
  	abstract public function insert($query); 
	//-----------------------------------------------------
	abstract public function update($query); 
	//-----------------------------------------------------
	public function delete($query) {
		self::update($query);
	}
	//----------------------------------------------------- 

	//***************************************************** 
	// Public Transaction Functions 
	//-----------------------------------------------------
	abstract public function begin();
	//-----------------------------------------------------
	abstract public function rollback();
	//-----------------------------------------------------
	abstract public function commit();
	//-----------------------------------------------------
	abstract public function lock($string);
	//-----------------------------------------------------
	abstract public function unlock(); 
} 
 
/******************************************************* 
 * Results Class 
 *******************************************************/ 
abstract class Result implements Iterator { 
  	protected $_results; 

	//***************************************************** 
	// Constructor and Database Operations Functions 
	//----------------------------------------------------- 
	function __construct($results) { 
		$this->_results = $results; 
	} 
	//----------------------------------------------------- 
	function __destruct() {
	}
	//----------------------------------------------------- 
	function __toString() {
		return print_r( $this, true );
	}
	//----------------------------------------------------- 

	//****************************************************
	// Interation
	//----------------------------------------------------
	//abstract public function rewind();
	//----------------------------------------------------
	//abstract public function current();
	//----------------------------------------------------
	//abstract public function key();
	//----------------------------------------------------
	//abstract public function next(); 
	//----------------------------------------------------
	//abstract public function valid();

	//***************************************************** 
	// Public Functions 
	//----------------------------------------------------- 
	abstract public function getAllRows();
	//----------------------------------------------------- 
	abstract public function getRow();
	//----------------------------------------------------- 
	abstract public function getRowAssoc();
	//----------------------------------------------------- 
	abstract public function getField($i);
	//----------------------------------------------------- 
	abstract public function getFields();
  	//----------------------------------------------------- 
	abstract function getNumFields(); 
	//----------------------------------------------------- 
	abstract function free();
	//-----------------------------------------------------
	abstract public function seek($i);
}
?>
